/**
 * Filename: MXMClass.java
 * Developer: Surim
 * Date: 8 March 2010
 * Description: Parse the MXM file that has been passed
 */

package MXitMXMConverter;
import java.io.*;
import java.util.*;

/**
 *
 * @author Steven Inskip
 */
public class MXMClass {
    public void MXMClass()
    {
        
    }

    // Generate the xml of the file
    protected String parseFile(String path)
    {
        try
        {
            FileReader text = new FileReader(path);
            int read;
            //String MXMFile = new String();

            read = text.read();

            if(read != -1)
            {
                ArrayList messages = new ArrayList();
                String magicNumber = new String();  // V2 4d584d2;
                String hdrlen = new String();
                int nummsgs = 0;


                for(int x = 0; x < 3; x++)
                {
                    String buffer = new String();

                    for(int y = 0; y < 4; y++)
                    {
                        buffer += Integer.toHexString((int) read);
                        read = text.read();
                    }

                    switch(x)
                    {
                        // Magic number
                        case 0:
                            magicNumber = buffer;
                            break;
                        // hdrlen
                        case 1:
                            hdrlen = buffer;
                            break;
                        // nummsgs
                        case 2:
                            nummsgs = Integer.parseInt(buffer, 16);
                            break;
                    }
                }

                if(magicNumber.equals(new String(magicNumber)))
                {
                    for(int x = 0; x < nummsgs; x++)
                    {
                        MXitMessageV2 msg = new MXitMessageV2();
                        String buffer = new String();
                        int stringCount = 0;

                        // Get entire message lenth
                        for(int y = 0; y < 4; y++)
                        {
                            buffer += Integer.toHexString((int) read);
                            read = text.read();
                        }
                        msg.len = Integer.parseInt(buffer, 16);

                        // Get timestamp
                        buffer = new String();
                        for(int y = 0; y < 4; y++)
                        {
                            buffer += Integer.toHexString((int) read);
                            read = text.read();
                        }
                        msg.timestamp = Long.parseLong(buffer, 16);

                        // Random 4 bytes????
                        for(int y = 0; y < 4; y++)
                        {
                            read = text.read();
                        }

                        // Get from alias
                        // Big endian for message length
                        buffer = new String();
                        for(int y = 0; y < 2; y++)
                        {
                            buffer += Integer.toHexString((int) read);
                            read = text.read();
                        }
                        stringCount = Integer.parseInt(buffer, 16);
                        
                        // Get alias name text
                        buffer = new String();
                        for(int y = 0; y < stringCount; y++)
                        {
                            buffer += (char) read;
                            read = text.read();
                        }
                        msg.fromAlias = buffer;





                        // Get to alias
                        // Big endian for message length
                        buffer = new String();
                        for(int y = 0; y < 2; y++)
                        {
                            buffer += Integer.toHexString((int) read);
                            read = text.read();
                        }
                        stringCount = Integer.parseInt(buffer, 16);

                        // Get alias name text
                        buffer = new String();
                        for(int y = 0; y < stringCount; y++)
                        {
                            buffer += (char) read;
                            read = text.read();
                        }
                        msg.toAlias = buffer;





                        // Get body
                        // Big endian for message length
                        buffer = new String();
                        for(int y = 0; y < 2; y++)
                        {
                            buffer += Integer.toHexString((int) read);
                            read = text.read();
                        }
                        stringCount = Integer.parseInt(buffer, 16);

                        // Get alias name text
                        buffer = new String();
                        for(int y = 0; y < stringCount; y++)
                        {
                            buffer += (char) read;
                            read = text.read();
                        }
                        msg.body = buffer;

                        // Get flags
                        for(int y = 0; y < 4; y++)
                        {
                            read = text.read();
                        }

                        System.out.println(msg.toString());
                    }
                }
            }
        }
        catch (IOException e)
        {

        }
        return new String("");
    }

    protected boolean generate(String path, String destinationPath)
    {
        return false;
    }
}
